package com.leyou.auth.config;

import com.leyou.auth.interceptors.LoginInterceptor;
import com.leyou.auth.utils.JwtUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 虎哥
 */
//@ConditionalOnProperty(name = "ly.auth.enableJwt", havingValue = "true")
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {

    private JwtUtils jwtUtils;
    private ClientProperties properties;

    public MvcConfiguration(@Lazy JwtUtils jwtUtils, ClientProperties properties) {
        this.jwtUtils = jwtUtils;
        this.properties = properties;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor(jwtUtils));
        // 判断是否存在要拦截的路径
        if(!CollectionUtils.isEmpty(properties.getIncludePathPatterns())) {
            registration.addPathPatterns(properties.getIncludePathPatterns());
        }
        // 判断是否存在放行的路径
        if(!CollectionUtils.isEmpty(properties.getExcludePathPatterns())) {
            registration.excludePathPatterns(properties.getExcludePathPatterns());
        }
    }
}
